package com.tjetc.service.impl;

import com.tjetc.common.JsonResult;
import com.tjetc.dao.FavouriteMapper;
import com.tjetc.entity.Folder;
import com.tjetc.service.FavouriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FavouriteServiceImpl implements FavouriteService {

    @Autowired
    private FavouriteMapper favouriteMapper;
    private String getTableName(Long type) {
        switch (type.intValue()) {
            case 3: return "cities";
            case 4: return "scenic_spots";
            case 5: return "travel_notes";
            case 6: return "guides";
            default: throw new IllegalArgumentException("无效的 type：" + type);
        }
    }

    // 根据 type 获取对应的 ID 列名
    private String getIdColumn(Long type) {
        switch (type.intValue()) {
            case 3: return "city_id";
            case 4: return "spot_id";
            case 5: return "travel_note_id";
            case 6: return "guide_id";
            default: throw new IllegalArgumentException("无效的 type：" + type);
        }
    }
    // 添加收藏

    @Override
    public JsonResult addFavourite(Long folderId, Long type, Long bindId, Long userId) {
        Map<String, Object> param = new HashMap<>();

        if (folderId == -1) {
            Long defaultFolderId = favouriteMapper.findDefaultfolder(userId);
            folderId = defaultFolderId;
        }

        // 填入参数
        param.put("folderId", folderId);
        param.put("type", type);
        param.put("bindId", bindId);
        param.put("userId", userId);

        // 插入收藏并回填 favouriteId
       Long insertResult = favouriteMapper.insertFavourite(param);

        // 获取返回的自增主键（解决 BigInteger cannot be cast to Long 问题）
        Object idObj = param.get("favouriteId");
        Long favouriteId = null;
        if (idObj instanceof BigInteger) {
            favouriteId = ((BigInteger) idObj).longValue();
        } else if (idObj instanceof Number) {
            favouriteId = ((Number) idObj).longValue();
        }


        // 查询内容数据
        Map<String, Object> contentData = favouriteMapper.findContentData(
                getTableName(type), getIdColumn(type), bindId
        );

        if (contentData != null && favouriteId != null) {
            String title = (String) contentData.get("title");
            String description = (String) contentData.get("description");
            String imagePath = (String) contentData.get("imagePath");
            favouriteMapper.updateFavourite(favouriteId, title, description, imagePath);
        }

        return insertResult > 0 ? JsonResult.success("收藏成功") : JsonResult.fail("收藏失败");
    }

    // 插入默认收藏夹
    @Override
    public JsonResult insertDefaultFolders(Long userId) {
        int result = favouriteMapper.insertDefaultFolders(userId);
        return result > 0 ? JsonResult.success("默认收藏夹创建成功") : JsonResult.fail("默认收藏夹创建失败");
    }

    // 插入新收藏夹
    @Override
    public JsonResult insertFolder(Long userId, String name) {
        int result = favouriteMapper.insertFolder(userId, name);
        return result > 0 ? JsonResult.success("收藏夹创建成功") : JsonResult.fail("收藏夹创建失败");
    }

    // 删除收藏夹
    @Override
    public JsonResult deleteFolder(Long folderId) {
        int result = favouriteMapper.deleteFolder(folderId);
        return result > 0 ? JsonResult.success("收藏夹删除成功") : JsonResult.fail("收藏夹删除失败");
    }

    // 根据收藏夹 ID 删除所有收藏项
    @Override
    public JsonResult deleteFavoritesByFolderId(Long folderId) {
        int result = favouriteMapper.deleteFavoritesByFolderId(folderId);
        return result > 0 ? JsonResult.success("收藏夹内所有收藏项删除成功") : JsonResult.fail("删除失败或收藏夹为空");
    }

    // 删除指定收藏项
    @Override
    public JsonResult removeFavorite(Long folderId, Long type, Long bindId,Long userId) {
        int result ;
        if (folderId == null) {
            Long fid=favouriteMapper.findFolder(userId,type,bindId);
            result = favouriteMapper.removeFavorite(fid, type, bindId);
        }else{
           result = favouriteMapper.removeFavorite(folderId, type, bindId);
        }

        return result > 0 ? JsonResult.success("收藏项移除成功") : JsonResult.fail("收藏项移除失败");
    }
    public JsonResult isFavorite(Long userId, Long bindId,Long type) {
        int r=favouriteMapper.findSpotfavourite(userId, bindId,type);

        return JsonResult.success(r);
    }

    @Override
    public JsonResult userFavourite(Long folderId, String name, Long userId) {
        List<Folder> folders=favouriteMapper.findUserFavourite(folderId,name,userId);
        return JsonResult.success(folders);
    }
}
